package client;

public class Model {
	private String host;
	private int port;
	private String text;
	private String pressedButton;
	private String operationCode;
	private String messageToServer;

	private String roomID;
	private String playerID;
	private String playersInGame;
	private String bankroll;
	private String smallBlind;
	private String bigBlind;
	private int nrOfSelectedCards = 0;
	private String selectedCardsIndex = "";

	private String[] card = new String[4];
	private String iDOfPlayerWithDealer;
	private String iDOfPlayerWithBigBlind;
	private String iDOfPlayerWithSmallBlind;
	private int playerBankrollNow;
	private String bettingRoundNumber = "0";
	private int maxBetNow;
	private int playerBetNow;
	private String betValue;
	private String raiseValue;
	private boolean wasBetPlayed = true;
	private boolean wasRaisePlayed = false;
	private String iDOfPlayerWhoPlayed;
	private String betValueOfPlayerWhoPlayed;
	private String bankrollOfPlayerWhoPlayed;
	private String iDOfPlayerWhoExchanged;
	private String nrOfExchangedCards;
	private String winnerID;
	private String winningAmount;
	private int numberOfWinners;
	private String winnersID;
	private String looserID;

	/**
	 * This method returns IP of server.
	 * 
	 * @return server IP
	 */
	public String getHost() {
		return host;
	}

	/**
	 * This method set IP number of server.
	 * 
	 * @param host
	 *            server IP
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * This method returns number of port.
	 * 
	 * @return port number
	 */
	public int getPort() {
		return port;
	}

	/**
	 * This method set port number to connect to server.
	 * 
	 * @param port
	 *            port number
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * This method returns some text.
	 * 
	 * @return text some text
	 */
	public String getText() {
		return text;
	}

	/**
	 * This method set some text.
	 * 
	 * @param text
	 *            some text
	 */
	public void setText(String text) {
		this.text = text;
	}

	/**
	 * This method returns name of pressed button.
	 * 
	 * @return pressedButton name of button that was clicked
	 */
	public String getPressedButton() {
		return pressedButton;
	}

	/**
	 * This method returns name of pressed button.
	 * 
	 * @return pressedButton name of button that was clicked
	 */
	public void setPressedButton(String buttonName) {
		this.pressedButton = buttonName;
	}

	/**
	 * This method returns user ID.
	 * 
	 * @return playerID userID to communicate with server
	 */
	public String getPlayerID() {
		return playerID;
	}

	/**
	 * This method set user ID get from server.
	 * 
	 * @param playerID
	 *            user ID
	 */
	public void setPlayerID(String playerID) {
		this.playerID = playerID;
	}

	/**
	 * This method returns how many players will take part in this game.
	 * 
	 * @return playersInGame number of players that will take part in this game
	 */
	public String getPlayersInGame() {
		return playersInGame;
	}

	/**
	 * This method set how many players will take part in this game.
	 * 
	 * @param playersInGame
	 *            number of players that will take part in this game
	 */
	public void setPlayersInGame(String playersInGame) {
		this.playersInGame = playersInGame;
	}

	/**
	 * This method returns amount of all player tokens.
	 * 
	 * @return bankroll amount of all player tokens
	 */
	public String getBankroll() {
		return bankroll;
	}

	/**
	 * This method set amount of all player tokens.
	 * 
	 * @param bankroll
	 *            amount of all player tokens
	 */
	public void setBankroll(String bankroll) {
		this.bankroll = bankroll;
	}

	/**
	 * This method returns room ID.
	 * 
	 * @return roomID ID of room
	 */
	public String getRoomID() {
		return roomID;
	}

	/**
	 * This method set room ID.
	 * 
	 * @param roomID
	 *            ID of room
	 */
	public void setRoomID(String roomID) {
		this.roomID = roomID;
	}

	/**
	 * This method returns value of small blind.
	 * 
	 * @return smallBlind small blind value
	 */
	public String getSmallBlind() {
		return smallBlind;
	}

	/**
	 * This method set value of small blind.
	 * 
	 * @param smallBlind
	 *            small blind value
	 */
	public void setSmallBlind(String smallBlind) {
		this.smallBlind = smallBlind;
	}

	/**
	 * This method returns value of big blind.
	 * 
	 * @return bigBlind big blind value
	 */
	public String getBigBlind() {
		return bigBlind;
	}

	/**
	 * This method set value of big blind.
	 * 
	 * @param bigBlind
	 *            big blind value
	 */
	public void setBigBlind(String bigBlind) {
		this.bigBlind = bigBlind;
	}

	/**
	 * This method individualise and set parameters of game from first message
	 * from server.
	 * 
	 * @param messageFromServer
	 *            content of message from server
	 */
	public void setParameters(String messageFromServer) {
		roomID = messageFromServer.substring(1, 2);
		playerID = messageFromServer.substring(8, 9);
		playersInGame = messageFromServer.split(",")[1];
		bankroll = messageFromServer.split(",")[2];
		playerBankrollNow = Integer.parseInt(bankroll);
		smallBlind = messageFromServer.split(",")[3];
		bigBlind = messageFromServer.split(",")[4];
		bigBlind = bigBlind.substring(0, bigBlind.length() - 1);
	}

	/**
	 * This method returns code of operation.
	 * 
	 * @return operationCode code of operation
	 */
	public String getOperationCode() {
		return operationCode;
	}

	/**
	 * This method set code of operation.
	 * 
	 * @param operationCode
	 *            code of operation
	 */
	public void setOperationCode(String operationCode) {
		this.operationCode = operationCode;
	}

	/**
	 * This method returns content of message that client send to server.
	 * 
	 * @return messageToServer content of message that client send to server
	 */
	public String getMessageToServer() {
		return messageToServer;
	}

	/**
	 * This method set content of message that client send to server.
	 * 
	 * @param messageToServer
	 *            content of message that client send to server
	 */
	public void setMessageToServer(String messageFromUser) {
		this.messageToServer = messageFromUser;
	}

	/**
	 * This method set user cards in a hand.
	 * 
	 * @param messageWithCards
	 *            message that contains cards written by digits
	 */
	public void setHand(String messageWithCards) {
		String onlyCards = messageWithCards.substring(8,
				messageWithCards.length() - 1);
		for (int i = 0; i < 4; i++) {
			card[i] = onlyCards.split("/")[i];
			card[i] = cardInNumbersToCardText(card[i]);
		}
	}

	public String getCardName(int i) {
		return card[i];
	}

	public String cardInNumbersToCardText(String cardInNumbers) {
		String cardColor = cardInNumbers.split(",")[0];
		if (cardColor.equals("1")) {
			cardColor = "spades";
		} else if (cardColor.equals("2")) {
			cardColor = "hearts";
		} else if (cardColor.equals("3")) {
			cardColor = "diamonds";
		} else if (cardColor.equals("4")) {
			cardColor = "clubs";
		} else {
			System.out.println("Converting color card error");
		}
		String cardSymbol = cardInNumbers.split(",")[1];
		if (cardSymbol.equals("1")) {
			cardSymbol = "AS";
		} else if (cardSymbol.equals("11")) {
			cardSymbol = "JACK";
		} else if (cardSymbol.equals("12")) {
			cardSymbol = "QUEEN";
		} else if (cardSymbol.equals("13")) {
			cardSymbol = "KING";
		}
		String cardInText = cardSymbol + " of " + cardColor;
		return cardInText;
	}

	public int getNrOfSelectedCards() {
		return nrOfSelectedCards;
	}

	public void setNrOfSelectedCards(int nrOfSelectedCards) {
		this.nrOfSelectedCards = nrOfSelectedCards;
	}

	public String getIndexOfSelectedCards() {
		return selectedCardsIndex;
	}

	public void setIndexOfSelectedCards(String selectedCardIndex) {
		selectedCardsIndex += selectedCardIndex;
	}

	public void forgetAboutSelection() {
		selectedCardsIndex = "";
		nrOfSelectedCards = 0;
	}

	public void setDealer(String iDOfPlayerWithDealer) {
		this.iDOfPlayerWithDealer = iDOfPlayerWithDealer;
		// System.out.println("iDOfPlayerWithDealer: "+ iDOfPlayerWithDealer);
	}

	public String getDealer() {
		return iDOfPlayerWithDealer;
	}

	public void setWhoHasBigBlind(String iDOfPlayerWithBigBlind) {
		if (iDOfPlayerWithBigBlind.equals(playerID)) {
			playerBankrollNow = Integer.parseInt(bankroll)
					- Integer.parseInt(bigBlind);
			playerBetNow = Integer.parseInt(bigBlind);
			// System.out.println("playerBankrollNow: " +
			// getPlayerBankrollNow());
		}
		this.iDOfPlayerWithBigBlind = iDOfPlayerWithBigBlind;
		// System.out.println("iDOfPlayerWithBigBlind: "+
		// iDOfPlayerWithBigBlind);
	}

	public String getWhoHasBigBlind() {
		return iDOfPlayerWithBigBlind;
	}

	public void setWhoHasSmallBlind(String iDOfPlayerWithSmallBlind) {
		if (iDOfPlayerWithSmallBlind.equals(playerID)) {
			playerBankrollNow = Integer.parseInt(bankroll)
					- Integer.parseInt(smallBlind);
			playerBetNow = Integer.parseInt(smallBlind);
			// System.out.println("playerBankrollNow: " +
			// getPlayerBankrollNow());
		}
		this.iDOfPlayerWithSmallBlind = iDOfPlayerWithSmallBlind;
		// System.out.println("iDOfPlayerWithSmallBlind: "+
		// iDOfPlayerWithSmallBlind);
	}

	public String getWhoHasSmallBlind() {
		return iDOfPlayerWithSmallBlind;
	}

	public int getPlayerBankrollNow() {
		return playerBankrollNow;
	}

	public void setPlayerBankrollNow(int playerBankrollNow) {
		this.playerBankrollNow = playerBankrollNow;
	}

	/**
	 * Sets number of betting round. If betting round change sets that bet was
	 * not played yet. It's necessary to display button bet in MainView in next
	 * rounds.
	 * 
	 * @param messageFromServer
	 *            message from server with operation code "06" with betting
	 *            round number inside
	 */
	public void setBettingRound(String messageFromServer) {
		if (!(this.bettingRoundNumber.equals(messageFromServer
				.substring(11, 12)))) {
			this.bettingRoundNumber = messageFromServer.substring(11, 12);
			wasBetPlayed = false;
		}
	}

	public String getBettingRound() {
		return bettingRoundNumber;
	}

	public void setMaxBetNow(String maxBetValue) {
		this.maxBetNow = Integer.parseInt(maxBetValue);
	}

	public int getMaxBetNow() {
		return maxBetNow;
	}

	public void setPlayerBetNow(String playerBetValue) {
		this.playerBetNow = Integer.parseInt(playerBetValue);
	}

	public int getPlayerBetNow() {
		return playerBetNow;
	}

	public int getCallValue() {
		return (maxBetNow - playerBetNow);
	}

	public void setBetValue(String betValue) {
		this.betValue = betValue;
	}

	public String getBetValue() {
		return betValue;
	}

	public void setRaiseValue(String raiseValue) {
		this.raiseValue = raiseValue;
	}

	public String getRaiseValue() {
		return raiseValue;
	}

	public void setWasBetPlayed(boolean wasBetPlayed) {
		this.wasBetPlayed = wasBetPlayed;
	}

	public boolean getWasBetPlayed() {
		return wasBetPlayed;
	}

	public void setWasRaisePlayed(boolean wasRaisePlayed) {
		this.wasRaisePlayed = wasRaisePlayed;
	}

	public boolean getWasRaisePlayed() {
		return wasRaisePlayed;
	}

	public void setIDOfPlayerWhoPlayed(String iDOfPlayerWhoPlayed) {
		this.iDOfPlayerWhoPlayed = iDOfPlayerWhoPlayed;
	}

	public String getIDOfPlayerWhoPlayed() {
		return iDOfPlayerWhoPlayed;
	}

	public void setBetValueOfPlayerWhoPlayed(String betValueOfPlayerWhoPlayed) {
		this.betValueOfPlayerWhoPlayed = betValueOfPlayerWhoPlayed;
	}

	public int getBetValueOfPlayerWhoPlayed() {
		return Integer.parseInt(betValueOfPlayerWhoPlayed);
	}

	public void setBankrollOfPlayerWhoPlayed(String bankrollOfPlayerWhoPlayed) {
		bankrollOfPlayerWhoPlayed = bankrollOfPlayerWhoPlayed.substring(0,
				bankrollOfPlayerWhoPlayed.length() - 1);
		this.bankrollOfPlayerWhoPlayed = bankrollOfPlayerWhoPlayed;
	}

	public String getBankrollOfPlayerWhoPlayed() {
		return bankrollOfPlayerWhoPlayed;
	}

	public void setPlayerIDWhoExchanged(String receivedExchangeMessage) {
		this.iDOfPlayerWhoExchanged = receivedExchangeMessage.substring(10, 11);
	}

	public String getPlayerIDWhoExchanged() {
		return iDOfPlayerWhoExchanged;
	}

	public void setNrOfExchangedCards(String receivedExchangeMessage) {
		this.nrOfExchangedCards = receivedExchangeMessage.substring(8, 9);
	}

	public String getNrOfExchangedCards() {
		return nrOfExchangedCards;
	}

	public void setWinnerID(String receivedWinningMessage) {
		this.winnerID = receivedWinningMessage.substring(11, 12);
	}

	public String getWinnerID() {
		return winnerID;
	}

	public void setWinningAmount(String receivedWinningMessage) {
		receivedWinningMessage = receivedWinningMessage.split(",")[2];
		this.winningAmount = receivedWinningMessage.substring(0, receivedWinningMessage.length() - 1); 
	}

	public String getWinningAmount() {
		return winningAmount;
	}

	public void setNumberOfWinners(String receivedWinningMessage) {
		receivedWinningMessage = receivedWinningMessage.split(",")[1]; //30
		this.numberOfWinners = receivedWinningMessage.length();
	}

	public int getNumberOfWinners() {
		return numberOfWinners;
	}

	public void setWinnersID(String receivedWinningMessage) {
		receivedWinningMessage = receivedWinningMessage.split(",")[1];
		this.winnersID = receivedWinningMessage;
	}

	public String getWinnersID() {
		return winnersID;
	}

	public void setPlayerBankrollAfterWinning(int winningAmount) {
		playerBankrollNow += winningAmount;
	}

	public void setLooserID(String receivedLooserMessage) {
		this.looserID = receivedLooserMessage.substring(11, 12);
	}

	public String getLooserID() {
		return looserID;
	}
	
	public void setModelToDefault() {
		//TODO set all variables to default except game parameters, playerBankrollNow
		this.bettingRoundNumber = "0";
		this.nrOfSelectedCards = 0;
		this.selectedCardsIndex = "";
		this.wasBetPlayed = true;
		this.wasRaisePlayed = false;
		this.playerBetNow = 0;
		this.maxBetNow = 0;
	}

}
